package atpa.partial;

public class StivaL {

    Nod t;
    int dim;

    public StivaL(int dim) {
        this.t = null;
        this.dim = dim;
    }

    public void push(int val) {
        if (!this.isFull()) {
            Nod z = new Nod(val);
            z.urm = t;
            t = z;
        } else {
            System.out.println("ERROR: Stack full");
        }
    }

    public int pop() {
        if (!this.isEmpty()) {
            int val = t.val;
            t = t.urm;
            return val;
        } else {
            System.out.println("ERROR: Stack empty");
            return -1;
        }
    }

    public int peek() {
        if (t != null) {
            return t.val;
        } else {
            return -1;
        }
    }

    public boolean isFull() {
        Nod x = t;
        int i = 1;
        while (x != null) {
            x = x.urm;
            i++;
        }
        return ((i - 1) == dim);
    }

    public boolean isEmpty() {
        return (t == null);
    }

    public void show() {
        if(t==null){
            System.out.println("ERROR: Stack Empty");
            return;
        }
        Nod x = this.t;
        int i = 1;
        int val_urm;
        do {
            if (x.urm == null) {
                val_urm = -1;
            } else {
                val_urm = x.urm.val;
            }
            System.out.println("Nod " + i + " val=" + x.val + " urm=" + val_urm);
            x = x.urm;
            i++;
        } while (x != null);
        System.out.println("-----------------");
    }
}
